package com.gitee.feizns.explore.data_structure.linked_list.singly.remove_ele;

/**
 * 移除链表元素
 *
 * 删除链表中等于给定值 val 的所有节点。
 * @author feizns
 * @since 2019/9/30
 */
public class RemoveEle {

    public static void main(String[] args) {
        ListNode node = ListNode.create();
        removeElements(node, 1).print();
    }

    public static ListNode removeElements(ListNode head, int val) {
        ListNode prev = null;
        ListNode curt = head;

        while ( curt != null ) {
            if ( curt.val == val )
                if ( prev == null )
                    head = head.next;
                else
                    prev.next = curt.next;
            else
                prev = curt;
            curt = curt.next;
        }

        return head;
    }

    private static ListNode remove(ListNode node, int val) {
        if ( node.next != null && node.next.val == val )
            node.next = node.next.next;
        return node.next;
    }

}
